package com.kaisa.activity.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.kaisa.activity.R;
import com.kaisa.activity.adapter.SnapAdapter;
import com.kaisa.activity.bean.Snap;
import com.kaisa.activity.constant.UrlHttp;
import com.kaisa.activity.utils.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class BuyActivity extends AppCompatActivity implements View.OnClickListener{

    @Bind(R.id.buy_back)
    ImageView buyBack;
    @Bind(R.id.buy_title)
    TextView buyTitle;
    @Bind(R.id.buy_lv)
    ListView buyLv;
    private SnapAdapter adapter;
    private List<Snap> list = new ArrayList<Snap>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        buyTitle.setText(title);
        initView();
        initData();
    }

    private void initView() {
        adapter = new SnapAdapter(list,this);
        buyLv.setAdapter(adapter);
    }

    private void initData() {
        buyBack.setOnClickListener(this);
        RequestParams params = new RequestParams(UrlHttp.SNAP_URL);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                list.addAll(JsonUtils.jsonData(s));
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buy_back:
                this.finish();
                break;
        }
    }
}
